package cn.ml.duoXianCheng.demos;

import java.util.ArrayList;
import java.util.List;

public class Zheng2 {
    public static List<String> manTou = new ArrayList<>();
    public static void main(String[] args) {

        new Zheng().start();
        new Chi().start();
        new Chi().start();
        new Chi().start();
    }

    static class Zheng extends Thread{
        @Override
        public void run() {
            while (true){
                synchronized ("a"){
                    for (int i = 0; i < 30; i++) {
                        Zheng2.manTou.add("第"+(i+1)+"个馒头");
                        System.out.println(Thread.currentThread().getName()+"已做好"+ Zheng2.manTou.get(i));
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    "a".notifyAll();
                    try {
                        "a".wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                synchronized ("b"){
                    System.out.println(Thread.currentThread().getName()+"蒸馒头的就去睡了");
                    try {
                        "b".wait();
                        System.out.println(Thread.currentThread().getName()+"没馒头了，开始做。。。");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }



            }
        }
    }

    static class Chi extends Thread{
        @Override
        public void run() {
            while (true){
                synchronized ("a"){
                    if (Zheng2.manTou.size() > 0){
                        String m = Zheng2.manTou.remove(0);
                        System.out.println(Thread.currentThread().getName()+"吃掉了"+ m);
                    }else {
                        
                        try {
                            synchronized ("b"){
                                System.out.println(Thread.currentThread().getName()+"发现没馒头了，叫蒸馒头的。。。");
                                "b".notify();
                            }
                            System.out.println(Thread.currentThread().getName()+"睡了");
                            "a".wait();
                            System.out.println(Thread.currentThread().getName()+"被叫醒了，说明有馒头了");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        "a".notifyAll();
                    }

                }
                //消化过程不能再锁里
                int rd = (int) (Math.random() * 1000);   //随机生成2000的随机数
                try {
                    Thread.sleep(rd);   //吃的过程消化rd毫秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}
